//
//  AppDelegate.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/19.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit
import SVProgressHUD

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
   

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //window设置
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        self.window?.backgroundColor = UIColor.whiteColor()
        
        setupDefaultRootVc()
//        self.window?.rootViewController = SHNewfeatureViewController()
        self.window?.makeKeyAndVisible()
        
        setupAppStyle()
        
        // 接收切换根控制器的通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVc:", name: WBSwitchRootViewControllerNoti, object: nil)
   
        return true
    }
    
    //MARK: - 设置rootViewController
    func setupDefaultRootVc(){
        // - 如果用户已经登录  rootVc -> WelcomeVc
        // - 如果用户没有登录  rootVc -> MainVc
        if SHUserAccountViewModel.sharedTools.isLogin {
            // 如果是新版本
            if isNewVersion() {
                self.window?.rootViewController = SHNewfeatureViewController()
            }else {
                // 如果不是新版本
                self.window?.rootViewController = SHWelcomeViewController()
            }
            
        }else {
            self.window?.rootViewController = SHMainViewController()
        }
    }
    
    // 切换根控制器
    func switchRootVc(noti: NSNotification){
        // 代表 OAuthVc -> WelComeVc
        if noti.object == nil {
            self.window?.rootViewController = SHWelcomeViewController()
        }else {
            // 代表WelComeVc -> MainVc
            // 由于当object 不为nil 切换的跟控制器均为MainVc
            self.window?.rootViewController = SHMainViewController()
        }
        
    }
    
    // 判断当前是否是最新版本
    func isNewVersion() -> Bool{
        // 01 获取当前的版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        // 把字符串转成double类型
        let currentVersionD = Double(currentVersion)!
        // 02 获取通过偏好设置保存的版本
        let shAppVersion = "shAppVersion"
        let oldVersion = NSUserDefaults.standardUserDefaults().doubleForKey(shAppVersion)
        // 03 保存获取到的当前版本
        NSUserDefaults.standardUserDefaults().setDouble(currentVersionD, forKey: shAppVersion)
        // 04 返回是否是最新版本
        return currentVersionD > oldVersion
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    //MARK: - App全局设置
    func setupAppStyle() {
        
        UITabBar.appearance().tintColor = K_THEME_COLOR
        UINavigationBar.appearance().tintColor = K_THEME_COLOR
        
        SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.Custom)
        SVProgressHUD.setBackgroundColor(K_THEME_COLOR)
        SVProgressHUD.setForegroundColor(UIColor.whiteColor())
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

